<template>
    <div v-if="visible" class="dialog-overlay">
        <div class="dialog">
            <div class="dialog-header">
                <button @click="closeDialog" class="close-button">×</button>
            </div>
            <div class="dialog-content" @click="jumpUrl">
                <img :src="ad_image"
                    alt="">
            </div>
        </div>
    </div>
</template>

<script lang="ts">
import { defineComponent, onMounted, ref } from 'vue';
import { useSiteConfig } from '../stores/siteConfig'
const siteConf = useSiteConfig()
export default defineComponent({
    name: 'AdDialog',
    setup() {
        const visible = ref(false);
        const dialogKey = 'adDialogState';
        const expirationTimeKey = 'adDialogExpirationTime';
        const expirationTime = 60 * 60 * 1000; // 1小时过期时间（毫秒）
        const ad_image = siteConf.ad.ad_image
        const loadDialogState = () => {
            const storedState = localStorage.getItem(dialogKey);
            const storedExpirationTime = localStorage.getItem(expirationTimeKey);
            if (storedState && storedExpirationTime) {
             
                const isOpen = JSON.parse(storedState) as boolean;
                
                const expirationTimeMillis = parseInt(storedExpirationTime, 10);
                const currentTimeMillis = Date.now();
                console.log(currentTimeMillis)
                if (currentTimeMillis < expirationTimeMillis) {
                    visible.value = isOpen;
                } else {
                    // 弹窗已过期，清除存储的状态和过期时间
                    localStorage.removeItem(dialogKey);
                    localStorage.removeItem(expirationTimeKey);
                }
            } else {
                // 如果没有存储的状态或过期时间，则根据默认配置设置弹窗状态
                visible.value = siteConf.ad.is_open;
                if (visible.value) {
                    console.log(expirationTime)
                    // 如果默认是打开的，则设置过期时间
                    const expirationDate = Date.now() + expirationTime;
                    localStorage.setItem(dialogKey, JSON.stringify(true));
                    localStorage.setItem(expirationTimeKey, expirationDate.toString());
                }
            }
        };

        const closeDialog = () => {
            visible.value = false;
            // 当用户关闭弹窗时，更新存储的状态为关闭，并设置过期时间
            localStorage.setItem(dialogKey, JSON.stringify(false));
            const expirationDate = (Date.now() + expirationTime);
            localStorage.setItem(expirationTimeKey, expirationDate.toString());
        };

        const jumpUrl = () => {
            window.open(siteConf.ad.ad_url);
            // 打开链接后，您可能希望立即关闭弹窗（取决于您的需求）
            closeDialog(); // 如果需要，可以在这里调用
        };

        onMounted(loadDialogState);

        return {
            visible,
            ad_image,
            jumpUrl,
            closeDialog,
        };
    },
});
</script>

<style scoped>
.dialog-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999;
}

.dialog {
    width: 600px;
    height: 600px;
    padding: 20px;
    border-radius: 5px;
    text-align: left;
    /* 或者保持 center，根据你的设计需求 */
    position: relative;
    /* 确保内部元素可以相对于 dialog 定位 */

}

.dialog-header {
    display: flex;
    justify-content: right;
    align-items: center;
    margin-bottom: 10px;
}

.close-button {
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgb(56, 56, 56, .5);
    border-radius: 100%;
    border: none;
    width: 40px;
    line-height: 40px;
    height: 40px;
    font-size: 2.5em;
    cursor: pointer;
    color: #ffffff;
}


.dialog-content {
    /* 内容区域的样式 */
    display: flex;
    justify-content: center;
    overflow: hidden;
    cursor: pointer;
    width: 100%;
}

.dialog-content img {
    width: 600px;
}

.dialog-footer {
    text-align: right;
    /* 底部按钮通常右对齐 */
    margin-top: 10px;
}
</style>